<template>
  <el-dialog :title="title" :close-on-click-modal="false" width='600px' :visible.sync="visible" class="JNPF-dialog JNPF-dialog_center" lock-scroll append-to-body>
    <el-form ref="dataForm" :model="dataForm" label-width="100px" :rules="fromRules">
      <template v-if="eventType==='transfer'||eventType==='assign'||eventType==='cancel' || eventType==='invoke' || eventType==='cc' || eventType==='multiAdd' || eventType==='assist' || eventType==='ccBatch' || eventType==='specialJump' ">
        <el-form-item label="指派节点" prop="nodeCode" v-if="eventType==='assign'">
          <el-select v-model="dataForm.nodeCode" placeholder="请选择指派节点">
            <el-option v-for="item in nodeModelList" :key="item.nodeCode" :label="item.nodeName" :value="item.nodeCode" />
          </el-select>
        </el-form-item>

        <el-form-item label="特送节点" prop="nodeCode" v-if="eventType==='specialJump'">
          <el-select v-model="dataForm.nodeCode" placeholder="请选择特送节点">
            <el-option v-for="item in nodeModelList" :key="item.id" :label="item.nodeName" :value="item.nodeCode" />
          </el-select>
        </el-form-item>

        <el-form-item :label="label+'给谁'" prop="freeApproverUserId" v-if="eventType==='transfer'||eventType==='assign'  || eventType==='invoke' || eventType==='cc' || eventType==='multiAdd' || eventType==='assist' " :rules="[{ required: true, message: `请选择${label}给谁`, trigger: 'click' }]">
          <JnpfUserSelect v-model="dataForm.freeApproverUserId" :placeholder="`请选择${label}给谁`" />
        </el-form-item>

        <el-form-item :label="label+'给谁'" prop="batchCCUserIds" v-if=" eventType==='ccBatch'  " :rules="[{ required: true, message: `请选择${label}给谁`, trigger: 'click' }]">
          <JnpfUserSelect v-model="dataForm.batchCCUserIds" :placeholder="`请选择${label}给谁`" multiple />
        </el-form-item>
        <el-switch v-model="dataForm.pointFlag" active-text="动态指定人" inactive-text="使用流程图中的人" v-if="eventType==='specialJump'" /><br />
        <el-form-item :label="label+'给谁'" prop="batchCCUserIds" v-if=" eventType==='specialJump' && dataForm.pointFlag === true " :rules="[{ required: true, message: `请选择${label}给谁`, trigger: 'click' }]">
          <JnpfUserSelect v-model="dataForm.batchCCUserIds" :placeholder="`请选择${label}给谁`" multiple />
        </el-form-item>

        <el-form-item :label="`${label}原因`" prop="handleOpinion" v-if="eventType==='assign'||eventType==='cancel'||(eventType==='transfer'&&properties&&properties.hasOpinion) || (eventType==='invoke'&&properties&&properties.hasOpinion)
            || (eventType==='cc'&&properties&&properties.hasOpinion) || (eventType==='multiAdd'&&properties&&properties.hasOpinion)  || (eventType==='assist'&&properties&&properties.hasOpinion) || (eventType==='specialJump'&&properties&&properties.hasOpinion) ">
          <el-input v-model="dataForm.handleOpinion" :placeholder="`请输入${label}原因`" type="textarea" :rows="4" />
        </el-form-item>
        <el-form-item :label="`${label}附件`" prop="fileList" v-if="eventType==='assign'||eventType==='cancel'||(eventType==='transfer'&&properties&&properties.hasOpinion)  || (eventType==='invoke'&&properties&&properties.hasOpinion)           || (eventType==='multiAdd'&&properties&&properties.hasOpinion)            || (eventType==='cc'&&properties&&properties.hasOpinion)    || (eventType==='assist'&&properties&&properties.hasOpinion)  || (eventType==='specialJump'&&properties&&properties.hasOpinion)   ">
          <JnpfUploadFile v-model="dataForm.fileList" :limit="3" />
        </el-form-item>
        <el-form-item label="手写签名" prop="signImg" v-if="properties.hasSign&&eventType==='transfer' || properties.hasSign&&eventType==='invoke'  || properties.hasSign&&eventType==='cc' || properties.hasSign&&eventType==='multiAdd' ||  properties.hasSign&&eventType==='assist' ||  properties.hasSign&&eventType==='specialJump' " :rules="[{ required: true, message: `请签名`, trigger: 'change' }]">
          <jnpf-sign v-model="dataForm.signImg" isInvoke />
        </el-form-item>

      </template>
      <template v-else>
        <template v-if="properties.hasOpinion">
          <el-form-item :label="`${label}原因`" prop="handleOpinion">
            <el-input v-model="dataForm.handleOpinion" :placeholder="`请输入${label}原因`" type="textarea" :rows="4" />
          </el-form-item>
          <el-form-item :label="`${label}附件`" prop="fileList">
            <JnpfUploadFile v-model="dataForm.fileList" :limit="3" />
          </el-form-item>
        </template>
        <el-form-item label="手写签名" prop="signImg" v-if="properties.hasSign" :rules="[{ required: true, message: `请签名`, trigger: 'change' }]">
          <jnpf-sign v-model="dataForm.signImg" isInvoke />
        </el-form-item>
      </template>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{$t('common.cancelButton')}}</el-button>
      <el-button type="primary" @click="handleSure()" :loading="btnLoading">
        {{$t('common.confirmButton')}}
      </el-button>
    </span>
  </el-dialog>
</template>
<script>
  import { mapGetters } from "vuex"
  export default {
    props: {
      assignNodeList: {
        type: Array,
        default: () => []
      },

      nodeModelList: {
        type: Array,
        default: () => []
      }
    },
    data() {
      return {
        visible: false,
        properties: {},
        eventType: '',
        dataForm: {
          handleOpinion: '',
          freeApproverUserId: '',
          batchCCUserIds: [],
          pointFlag: true,
          nodeCode: '',
          fileList: [],
          signImg: '',
        },
        fromRules: {
          nodeCode: [
            { required: true, message: '请选择指派节点', trigger: 'change' }
          ]
        },
        btnLoading: false,
        title: '',
        label: '',
      }
    },
    computed: {
      ...mapGetters(['userInfo'])
    },
    methods: {
      init(properties, eventType) {
        this.visible = true
        this.properties = properties
        this.eventType = eventType || ''
        this.dataForm.handleOpinion = ''
        this.dataForm.freeApproverUserId = ''
        this.dataForm.batchCCUserIds = []
        this.dataForm.pointFlag = true
        this.dataForm.nodeCode = ''
        this.dataForm.fileList = []
        this.dataForm.signImg = ""
        if (this.properties.hasSign) this.dataForm.signImg = this.userInfo.signImg
        switch (eventType) {
          case 'transfer':
            this.title = '转审'
            this.label = '转审'
            break;
          case 'invoke':
            this.title = '委派'
            this.label = '委派'
            break;
          case 'cc':
            this.title = '抄送'
            this.label = '抄送'
            break;
          case 'ccBatch':
            this.title = '批量抄送'
            this.label = '批量抄送'
            break;
          case 'multiAdd':
            this.title = '并加签'
            this.label = '并加签'
            break;
          case 'shareEx':
            this.title = '关注-取关'
            this.label = '关注-取关'
            break;
          case 'specialJump':
            this.title = '特送'
            this.label = '特送'
            break;
          case 'assist':
            this.title = '协办'
            this.label = '协办'
            break;
          case 'taskCancel':
            this.title = '终止'
            this.label = '终止'
            break;
          case 'revoke':
            this.title = '撤回流程'
            this.label = '撤回'
            break;
          case 'recall':
            this.title = '撤回审核'
            this.label = '撤回'
            break;
          case 'assign':
            this.title = '指派'
            this.label = '指派'
            break;
          case 'cancel':
            this.title = '终止审核不可恢复'
            this.label = '终止'
            break;
          case 'approvalCancel':
            this.title = '不同意'
            this.label = '不同意'
            break;
          case 'counterSignAbstention':
            this.title = '会签弃权'
            this.label = '会签弃权'
            break;
          case 'lockOrUnLock':
            this.title = '锁定/解锁'
            this.label = '锁定/解锁'
            break;
          default:
            break;
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      handleSure() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.btnLoading = true
            this.$emit('submit', this.dataForm)
            this.closeDialog()
          }
        })
      },
      closeDialog() {
        this.btnLoading = false
        this.visible = false
      },
    }
  }
</script>
